package com.sky.task;

import com.alibaba.fastjson.JSON;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义定时任务类
 */
@Component//交给spring管理
@Slf4j
public class MyTask {

    @Autowired
    private WebSocketServer webSocketServer;
    /**
     * 定时任务 每隔5秒触发一次
     */
//    @Scheduled(cron = "0/5 * * * * ?")
    public void executeTask(){
        //通过websocket向客户端浏览器推送消息 type orderId content
        System.out.println("开始发送测试消息");
        Map map = new HashMap();
        map.put("type",1); // 1表示来单提醒 2表示客户催单
        map.put("orderId","123");
        map.put("content","订单号：" + 123);

        String json = JSON.toJSONString(map);
        webSocketServer.sendToAllClient(json);
    }

}
